package com.ftwj.demo.modular.controller;

import java.util.List;
import java.util.Map;

import javax.validation.constraints.Positive;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.global.aop.RateLimit;
import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.message.impl.PlatformSend;
import com.ftwj.demo.modular.entity.MessageBean;
import com.ftwj.demo.modular.service.MessageService;
import com.ftwj.demo.modular.service.MessageUserService;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* <p>Title: MessageController</p>  
* <p>Description: 消息管理前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author HeYang
* @date 2020年4月10日 下午4:40:24
* @version V1.0
 */
@RestController 
@RequestMapping("/message")
@Api(tags="消息管理接口", value="消息增删改操作管理")
@Validated
public class MessageController {
	
	@Autowired
	private MessageService service;
	@Autowired
	private MessageUserService userService;
	@Autowired
	private PlatformSend sendMsg;

	/**
	 * <p>Title: msgList</p>  
	 * <p>Description: 全部消息分页查询</p>  
	 * @param current
	 * @param size
	 * @param type
	 * @author HeYang
	 * @date 2020年4月9日  
	 * @version 1.0
	 */
	@GetMapping("page")
	@ApiOperation(value="消息分页列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	public ResultBean<?> page(@RequestParam(required = true) Integer current, @RequestParam(required = true) Integer size,
			@RequestParam(name="title",required = false) String title) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,title);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 全部消息列表</p>  
	 * @param title
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("list")
	@RateLimit(limit=60)
	@ApiOperation(value="消息列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> list(@RequestParam(name="title",required = false) String title) {
		List<MessageBean> list = service.getList(title);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: mySendPage</p>  
	 * <p>Description: 我发送的消息分页列表</p>  
	 * @param current
	 * @param size
	 * @param title
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("/send/page")
	@ApiOperation(value="我发送的消息分页列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	public ResultBean<?> mySendPage(@RequestParam(required = true) Integer current,@RequestParam(required = true) Integer size,
			@RequestParam(name="title",required = false) String title) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.pageForMySend(pageInfo,title);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: mySendList</p>  
	 * <p>Description: 我发送的消息列表</p>  
	 * @param title
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("/send/list")
	@ApiOperation(value="我发送的消息列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> mySendList(@RequestParam(name="title",required = false) String title) {
		List<MessageBean> list = service.getMySendList(title);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: myRelatedPage</p>  
	 * <p>Description: 与我相关的消息分页列表</p>  
	 * @param current
	 * @param size
	 * @param title
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("/related/page")
	@ApiOperation(value="与我相关的消息分页列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
    	@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
    	,@ApiImplicitParam(name="size", value="每页显示条数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
    })
	public ResultBean<?> myRelatedPage(@RequestParam(required = true) Integer current, @RequestParam(required = true) Integer size,
			@RequestParam(name="title",required = false) String title) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.pageForMyRelated(pageInfo,title);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: myRelatedList</p>  
	 * <p>Description: 与我相关的消息列表</p>  
	 * @param title
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("/related/list")
	@ApiOperation(value="与我相关的消息列表", notes="数据",code=0,httpMethod=HttpMethod.GET)
	public ResultBean<?> myRelatedList(@RequestParam(name="title",required = false) String title) {
		List<Map<String, Object>> list = service.getMyRelatedList(title);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: insert</p>  
	 * <p>Description: 发送消息</p>  
	 * @param message
	 * @param userIds
	 * @param files
	 * @author HeYang
	 * @date 2020年4月9日  
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="用户消息新增", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> insert(@RequestBody@Validated MessageBean entity){
		//PlatfromEntity entity = PlatfromConvert.convert(message, userIds, files);
		sendMsg.send(entity);
		return ResultBean.success();
	}
	
	/**
	 * <p>Title: read</p>  
	 * <p>Description: 用户消息读取</p>  
	 * @param msgId
	 * @author HeYang
	 * @date 2020年4月9日  
	 * @version 1.0
	 */
	@PutMapping("/read")
	@ApiOperation(value="用户消息读取", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> read(@RequestParam(name="msgId")@Positive(message="ID必须是大于0的整数") Integer msgId){
		if(userService.read(msgId) >0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: dels</p>  
	 * <p>Description: 批量删除消息</p>  
	 * @param msgId
	 * @author HeYang
	 * @date 2020年4月9日  
	 * @version 1.0
	 */
	@DeleteMapping("/dels")
	@ApiOperation(value="用户消息删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	public ResultBean<?> dels(@RequestParam(name="msgIds") Integer[] msgIds){
		service.dels(msgIds);
		return ResultBean.success();
	}
	
	
}
